package com.ce.pms.charge.web;

import java.util.List;

import com.ce.pms.base.web.PmsBaseAction;
import com.ce.pms.charge.model.PeriodDomain;
import com.ce.pms.charge.service.IMeterDataRegisterService;
import com.ce.pms.charge.service.IMonthlyStatementService;
import com.ce.pms.charge.service.IPeriodLogService;
import com.ce.pms.charge.service.IPeriodService;
import com.ce.pms.charge.service.ITransactionService;
import com.ce.pms.charge.vo.MonthlyStatementSearchVO;
import com.ce.pms.charge.vo.OriginalMeterDataVO;
import com.ce.pms.charge.vo.PeriodVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.ce.pms.exception.UILayerException;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.components.xtable.util.ExtremeUtils;
import com.sitechasia.webx.core.annotation.Read;
import com.sitechasia.webx.core.support.Page;
//import com.sun.org.apache.xerces.internal.parsers.IntegratedParserConfiguration;

/**
 * 功能描述:收费月结
 *
 * @author 黄彦军
 *
 */
public class MonthlyStatementAction extends PmsBaseAction {
	//月结Service
	private IMonthlyStatementService monthlyStatementService;
	//收费周期Serivce
	private IPeriodService periodService ;
	//操作日志Serivce
	private IPeriodLogService periodLogService;
	//走表数据Serivce
	private IMeterDataRegisterService meterDataRegisterService;
	private ITransactionService transactionService;


	public ITransactionService getTransactionService() {
		return transactionService;
	}

	public void setTransactionService(ITransactionService transactionService) {
		this.transactionService = transactionService;
	}

	/**
	 * 功能描述:加载收费月结主页面  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-13  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	@SuppressWarnings("unchecked")
	public String initMonthlyStatementMain(
			@Read(key = "ec_crd",defaultValue="15")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo) {
		//加载已月结所有周期
		List<PeriodVO> periods = periodService.findPeriodByValidity(SysrefcodeDO.FEE_PERIOD_VALIDITY_INVALIDATION);
		this.getRequest().setAttribute("periods", periods);

		//判断是否第一次请求
		String isfirst = getRequest().getParameter("isfirst");
		if(!BlankUtil.isBlank(isfirst)){
			this.getRequest().setAttribute("currentlyPage", 1);
			this.getRequest().setAttribute("totalRows",0);
			return "monthlyStatementMain";
		}

		//获取查询条件
		String period = null;
		if (!BlankUtil.isBlank(getRequest().getParameter("period")))
		{
			period = getRequest().getParameter("period");
		}

		//获取排序字段
		String sortStr = ExtremeUtils.getOrderString(getRequest());
		if(!BlankUtil.isBlank(sortStr)){
			if ("endTime asc".equals(sortStr)){sortStr = "asc";
			}else{sortStr="desc";}
		}else{ sortStr = "desc";}

		//创建一个查询条件 VO
		MonthlyStatementSearchVO searchVO = new MonthlyStatementSearchVO(sortStr,pageNo,pageSize,null,period);

		//加载符合条件的月结列表
		Page page= monthlyStatementService.queryCurPageMonthlyStatement(searchVO);
		//查询总行数
		Long  totalRows = page.getTotalCount();

		this.getRequest().setAttribute("list", page.getResult());
		this.getRequest().setAttribute("currentlyPage", page.getCurrentPageNo());
		this.getRequest().setAttribute("totalRows",totalRows.intValue());
		this.getRequest().setAttribute("period", period);

		return "monthlyStatementMain";
	}

   /**
    * 功能描述: 加载收费流程弹出页面 <br>
    * @return
    * Create author:黄彦军 <br>
    * Create on:2008-8-27  <br>
    * Edit author:  <br>
    * Edit on:    <br>
    * Why:     <br>
    */
	public String chargeCycleForwardSmall(){

		return "chargeCycleForwardSmall";
	}

	/**
	 * 功能描述: 加载月结确认页面 <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-28  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initConfirmation(){
		List<PeriodVO> list = periodService.findPeriodByValidity(SysrefcodeDO.FEE_PERIOD_VALIDITY_VALID);

		PeriodVO PeriodVO  = list.get(0);
		//获取当前收费周期,并进行格式化( yyyy-MM )
		String period = PeriodVO.getPeriod();
		period = period.substring(0,4)+"-"+period.substring(4,6);
		setAttribute("period", period);
		return "confirmation";
	}

	/**
	 * 功能描述: 进行月结操作 <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-8-27  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public void monthlyStatement(){

		//查询所有有效的收费周期
		List<PeriodVO> list = periodService.findPeriodByValidity(SysrefcodeDO.FEE_PERIOD_VALIDITY_VALID);
		boolean flag;

		//是否都生成了通知单
		flag = periodLogService.checkNoticeLog(list);
		if(!BlankUtil.isBlank(flag) && flag == false){//否

			//是否都做了应收计算
			flag = periodLogService.checkReceivableLog(list);
			if(!BlankUtil.isBlank(flag) && flag == false){//否
				  this.renderText("{type:'receivableWarning'}");
				  return ;
			}

			this.renderText("{type:'noticeWarning'}");
			return ;
		}else{
			//检查所有的收费周期走表数据是否有新建状态的数据
			List<OriginalMeterDataVO> newDatas =meterDataRegisterService.getMeterDatasListByPeriodId(null);
			if (!BlankUtil.isBlank(newDatas) && newDatas.size()>0){//否
			  this.renderText("{type:'refreshChargeDatas'}");
			  return ;
			}

			//判断是否有出盘锁定状态的数据
			Integer nums = transactionService.getOutLocakTransactionCount();
			if (nums > 0){
				this.renderText("{type:'lockedWarning'}");
				return ;
			}

			//满足条件,进行月结
			String period="";
			try {
				period = monthlyStatementService.addMonthlyStatement(this.getUserId());
			} catch (Exception e) {
				throw new UILayerException(e.getMessage(),BusinessException.FAILURE);
			}
	        this.renderText("{type:'success',period:"+period+"}");
		}

	}


	/**
	 * 功能描述:加载走表数据为新建状态的列表页面  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-1  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String initNewsShargeDatasList(
			@Read(key = "ec_crd",defaultValue="15")	int pageSize,
            @Read(key = "ec_p", defaultValue="1")	int pageNo){
		List<OriginalMeterDataVO> list= meterDataRegisterService.getMeterDatasListByPeriodId(null);

		setAttribute("currentlyPage", 1);
		setAttribute("totalRows",list.size());
		setAttribute("list", list);
		return "newsShargeDatasList";
	}

	/**
	 * 功能描述:加载走表数据有新建状态的提示页面  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-2  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String newsShargeDatasWarning(){
		return "newsShargeDatasWarning";
	}

	/**
	 * 功能描述:进行月结操作跳转  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-9-1  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public String ongoing(){
		setAttribute("flag", "ongoing");
		return "onlyRefresh";
	}

//-----------------------------------------------------------------------------
	public IMonthlyStatementService getMonthlyStatementService() {
		return monthlyStatementService;
	}

	public void setMonthlyStatementService(
			IMonthlyStatementService monthlyStatementService) {
		this.monthlyStatementService = monthlyStatementService;
	}

	public IMeterDataRegisterService getMeterDataRegisterService() {
		return meterDataRegisterService;
	}

	public void setMeterDataRegisterService(
			IMeterDataRegisterService meterDataRegisterService) {
		this.meterDataRegisterService = meterDataRegisterService;
	}

	public IPeriodLogService getPeriodLogService() {
		return periodLogService;
	}

	public void setPeriodLogService(IPeriodLogService periodLogService) {
		this.periodLogService = periodLogService;
	}

	public IPeriodService getPeriodService() {
		return periodService;
	}

	public void setPeriodService(IPeriodService periodService) {
		this.periodService = periodService;
	}



}
